package com.biomatch.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.biomatch.domain.Fingerprint;
import com.biomatch.domain.Group;
import com.biomatch.domain.UserInfo;
import com.biomatch.dto.FingerprintCount;
import com.biomatch.dto.Pagination;
import com.biomatch.mapper.FingerprintMapper;
import com.biomatch.service.FingerprintService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class FingerprintServiceImpl implements FingerprintService {

    @Autowired
    FingerprintMapper fingerprintMapper;

    @Override
    public IPage<Fingerprint> getFingerprintByPage(Pagination pagination) {
        Page<Fingerprint> page = new Page<>(pagination.getCurrent(), pagination.getPageSize());
        return fingerprintMapper.selectPage(page, null);
    }

    @Override
    public List<Fingerprint> getAllFingerprint() {
        return fingerprintMapper.selectList(null);
    }

    @Override
    public Fingerprint saveOrUpdate(Fingerprint fingerprint) {
        //如果id<=0 判断增加
        if(fingerprint.getId()<=0)
        {
//            QueryWrapper<Fingerprint> fingerprintQueryWrapper = new QueryWrapper<>();
//            fingerprintQueryWrapper.eq("FingerPrintId",fingerprint.getFingerPrintId());
//            if(fingerprintMapper.selectCount(fingerprintQueryWrapper)>0)
//            {
//                return  null;
//            }
//            else {
                fingerprint.setCreateTime(new Date());
                fingerprintMapper.insert(fingerprint);
//            }
        }
        //id>0 判断更新
        else{
                fingerprint.setUpdateTime(new Date());
                fingerprintMapper.updateById(fingerprint);
        }
        return fingerprint;
    }

    @Override
    public Fingerprint deleteAllFingerPrints(Fingerprint fingerprint) {
        QueryWrapper<Fingerprint> fingerprintQueryWrapper = new QueryWrapper<>();
        fingerprintQueryWrapper.eq("UserId", fingerprint.getUserId());
        if (fingerprintMapper.selectCount(fingerprintQueryWrapper) > 0) {
            fingerprintMapper.deleteByUserId(fingerprint.getUserId());
            return fingerprint;
        } else
        {
            return null;
        }
    }

    @Override
    public IPage<FingerprintCount> getFingerprintCountAndUsersByPage(Pagination pagination) {
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("start",(pagination.getCurrent()-1) * pagination.getPageSize());
        params.put("pageSize",pagination.getPageSize());
        //获取总记录数据
        pagination.setTotal(fingerprintMapper.findUserCountByMap(params));

        IPage<FingerprintCount> iPage = new Page<>(pagination.getCurrent(), pagination.getPageSize(),pagination.getTotal());
        iPage.setRecords(fingerprintMapper.getFingerprintCountAndUsersByPage(params));


        return iPage;
    }

    @Override
    public List<Fingerprint> FindFingerPrintsByUser(String userId) {
        return fingerprintMapper.FindFingerPrintsByUser(userId);
    }

    @Override
    public Fingerprint deleteOneFingerprintById(Fingerprint fingerprint) {
        QueryWrapper<Fingerprint> fingerprintQueryWrapper = new QueryWrapper<>();
        fingerprintMapper.deleteById(fingerprint.getId());
        return fingerprint;
    }


}
